/**
 * 
 */
package com.xpec.c4.service.job;

import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RunnableScheduledFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 定时器
 * 
 * @author lipeilin
 * @2011-4-28 下午02:56:14
 *
 */
public class JobThreadPool extends ThreadPoolExecutor{
	
	private static final Log log = LogFactory.getLog(JobThreadPool.class);

	/**
	 * @param corePoolSize
	 */
	public JobThreadPool(int corePoolSize) {
		super(corePoolSize, corePoolSize,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>());
	}

	@Override
	protected void afterExecute(Runnable r, Throwable t) {
		if(r instanceof RunnableScheduledFuture){
			RunnableScheduledFuture<?> task = (RunnableScheduledFuture<?>)r;
			if(task.isDone()){
				try {
					task.get();
				} catch (InterruptedException e) {
					//log.error(r,e);
				} catch (ExecutionException e) {
					log.error(r,e);
				} catch (CancellationException e) {
				}
				remove(r);
			}
		}
		if(t!=null){
			log.error(r,t);
		}
		super.afterExecute(r, t);
	}

}
